<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateBoxInfo">添加分类</el-button>
      </div>
    <el-table
    border
    :data="cateList"
    style="width: 100%">
    <el-table-column
      label="序号"
      width="180">
      <template slot-scope="{row}">
        {{row.id}}
      </template>
    </el-table-column>
    <el-table-column
      label="分类名称"
      width="380">
      <template slot-scope="{row}">
            {{row.cate_name}}
      </template>
    </el-table-column>
      <el-table-column
      label="分类别名"
      width="450">
      <template slot-scope="{row}">
            {{row.cate_alias}}
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    </el-card>

    <!-- 添加分类的对话框 -->
<el-dialog @closed="$refs.addRef.resetFields()" :title="addTitle" :visible.sync="addVisible" width="35%">
    <!-- 添加的表单 -->
    <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
    <el-form-item label="分类名称" prop="cate_name">
        <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
    </el-form-item>
    <el-form-item label="分类别名" prop="cate_alias">
        <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
    </el-form-item>
    </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button size="mini" @click="addVisible = false">取 消</el-button>
    <el-button size="mini" type="primary" @click="addCate">{{addBtnName}}</el-button>
  </span>
</el-dialog>

  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      //   文章的分类列表
      cateList: [],
      // 控制添加对话框的显示与隐藏
      addVisible: false,
      addTitle: '',
      addBtnName: '',
      addForm: {
        cate_name: '',
        cate_alias: '',
        id: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', tigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', tigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类名称必须为字母数字！' }
        ]
      }
    }
  },
  methods: {
    async initCateList () {
      this.cateList = []
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return
      res.data.forEach(item => {
        this.cateList.push(item)
      })
    },
    addCateBoxInfo () {
      // 弹出对话框
      this.addVisible = true
      //   改为添加的标题
      this.addTitle = '添加文章分类'
      this.addBtnName = '添 加'
    },
    addCate () {
      // 兜底校验
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        if (this.addTitle === '添加文章分类') {
          const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
          if (res.code !== 0) return
        } else {
          const { data: res } = await this.$http.put('/my/cate/info', this.addForm)
          if (res.code !== 0) return
        }
        // 刷新列表
        this.initCateList()
        // 重置表单
        this.$refs.addRef.resetFields()
        // 关闭弹出层
        this.addVisible = false
      })
    },
    handleEdit (row) {
      // 弹出对话框
      this.addVisible = true
      //   改为修改的标题
      this.addTitle = '修改文章分类'
      this.addBtnName = '修 改'
      this.addForm.id = row.id
      this.addForm.cate_name = row.cate_name
      this.addForm.cate_alias = row.cate_alias
    },
    handleDelete (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        if (id === 1 || id === 2) return this.$message.error('不允许删除该分类！')
        //   根据id删除文章分类
        this.$http.delete(`/my/cate/del?id=${id}`).then(res => {
          if (res.data.code !== 0) return
          // 刷新列表
          this.initCateList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  created () {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
